﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Util;
using Org.BouncyCastle.Crypto;
using System.Collections.Generic;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.权限管理
{
    /// <summary>
    /// 角色
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly ILogRepository logRepository;

        public RoleController(IRoleRepository roleService, ILogRepository logRepository)
        {
            RoleService = roleService;
            this.logRepository = logRepository;
        }

        public IRoleRepository RoleService { get; }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="rnam"></param>
        /// <param name="state"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleList(string? rnam = "", int state = 3, int pageIndex = 1, int pageSize = 5)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var rlist = RoleService.GetRoleList(rnam, state, pageIndex, pageSize, out totalCount);

            if (rlist != null)
            {
                return Ok(BaseResult.Result(rlist, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRole(TRole m)
        {
            ApiResult apiResult = new ApiResult();
            bool IsOK = RoleService.AddRole(m);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "添加成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "添加失败！已存在角色编码！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelRole(int rid)
        {
            if(rid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            bool IsOK = RoleService.DelRole(rid);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "删除成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "删除失败！";
            apiResult.data = false;
            return Ok(apiResult);
        }
        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="ridList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelRoleList(int[] ridList)
        {
            if (ridList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            ApiResult apiResult = new ApiResult();
            foreach (var rid in ridList)
            {
                bool IsOK = RoleService.DelRole(rid);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog
                {
                    LogType = 1,
                    ActionType = 5,
                    ModuleName = "基础数据",
                    UserName = uname,
                    RealName = runame,
                    UserId = rid,
                    Description = IsOK ? uname + "删除了设备,id:" + rid : uname + "删除设备失败,id:" + rid,
                    //获取前端IP地址
                    Ipaddress = ip
                };
                logRepository.AddTLod(logg);
                if (!IsOK)
                {
                    daRes = false;
                    break;
                }
            }
            if (daRes)
            {
                apiResult.code = "200";
                apiResult.message = "删除成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "删除失败！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 根据主键获取角色
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult RoleById(int rid)
        {
            if(rid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();

            var robj = RoleService.RoleById(rid);

            if (robj != null)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功！";
                apiResult.data = robj;
                apiResult.count = 1;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "获取失败！";
            return Ok(apiResult);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdRole(TRole m)
        {
            if (m.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            bool IsOK = RoleService.UpdRole(m);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "修改成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "修改失败！已存在角色编码！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 权限分配列表显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleMenuActionList()
        {
            ApiResult apiResult = new ApiResult();
            var rlist = RoleService.GetRoleMenuActionList();

            if (rlist != null)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功！";
                apiResult.data = rlist;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "获取失败！";
            return Ok(apiResult);
        }
    }
}
